package com.h3c.nem.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.h3c.nem.dto.GenerateOfferDTO;
import com.h3c.nem.dto.SendEmailDTO;
import com.h3c.nem.service.ISapSyncService;
import com.h3c.nem.utils.EmailUtil;
import com.sap.conn.jco.JCoStructure;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author chenyifan
 * @date 2022/6/14 14:52
 */
@Slf4j
@AllArgsConstructor
@CrossOrigin
@RestController
@RequestMapping("/email")
public class EmailController {

    private final ISapSyncService sapSyncService;


    /**
     * 解析并填充模板回显
     *
     * @return
     */
    @PostMapping("/parseAndSendOfferEmail")
    public Result<String> parseAndShowEmailContent(@RequestBody GenerateOfferDTO generateOfferDTO) {
        try {
            JCoStructure returnStructure = sapSyncService.linkBpmGetData(generateOfferDTO.getWorkerId());
            //预备员工工资
            BigDecimal Betrg = returnStructure.getBigDecimal("BETRG");
            //月标准工资
            BigDecimal Z1jbx = returnStructure.getBigDecimal("Z1JBX");
            //年标准工资
            BigDecimal Z0020 = returnStructure.getBigDecimal("Z0020");
            //年目标浮动奖金
            BigDecimal Z0040 = returnStructure.getBigDecimal("Z0040");
            //目标浮动奖金比例
            BigDecimal Z0030 = returnStructure.getBigDecimal("Z0030");
            //目标总现金包
            BigDecimal Znmbzje = returnStructure.getBigDecimal("ZNMBZJE");
            HashMap<String, String> salaryParams = new HashMap<>(7);
            salaryParams.put("Betrg", Objects.nonNull(Betrg) ? Betrg.toString() : "");
            salaryParams.put("Z1jbx", Objects.nonNull(Betrg) ? Z1jbx.toString() : "");
            salaryParams.put("Z0020", Objects.nonNull(Betrg) ? Z0020.toString() : "");
            salaryParams.put("Z0040", Objects.nonNull(Betrg) ? Z0040.toString() : "");
            salaryParams.put("Z0030", Objects.nonNull(Betrg) ? Z0030.toString() : "");
            salaryParams.put("Znmbzje", Objects.nonNull(Betrg) ? Znmbzje.toString() : "");
            salaryParams.put("sendTime", DateUtil.format(new Date(),"yyyy-MM-dd"));
            return new Result(true, StatusCodeEnum.NORMAL, EmailUtil.fixUpContent(generateOfferDTO, salaryParams));
        } catch (Exception e) {
            log.error("解析填充模板失败,原因:{}", JSONUtil.toJsonStr(e));
            return new Result(true, 500, e.getMessage());
        }
    }


    /**
     * 发送邮件offer
     *
     * @return
     */
    @PostMapping("/sendOfferPtEmail")
    public Result<String> sendOfferPtEmail(@Valid @RequestBody SendEmailDTO sendEmailDTO) {
        try {
            EmailUtil.sendOfferPtEmail(sendEmailDTO);
            return new Result(true, StatusCodeEnum.NORMAL);
        } catch (Exception e) {
            log.error("发送邮件失败，原因:{}", JSONUtil.toJsonStr(e));
            return new Result(true, StatusCodeEnum.UNKNOW_ERROR.getCode(),e.getMessage());
        }
    }
}
